package com.qcq.wecheck.controller;

import com.qcq.wecheck.entity.Schedule;
import com.qcq.wecheck.entity.User;
import com.qcq.wecheck.result.CommonResult;
import com.qcq.wecheck.service.ExportService;
import com.qcq.wecheck.service.ScheduleService;
import com.qcq.wecheck.util.ExportUtils;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 * 报表输出 前端控制器
 * </p>
 *
 * @author O
 * @version 1.0
 * @since 2018/10/16
 */
@RestController
@RequestMapping("/export")
public class ExportController {
	@Resource
	private ExportService exportService;
	@Resource
	private ScheduleService scheduleService;

	/**
	 * 某考勤表的参加人员列表
	 */
	@GetMapping("/member")
	public void simple(@RequestParam("scheduleId") Long scheduleId,
	                   HttpServletResponse response) {
		String fileName = "参与人员信息.xls";

		List<User> users = scheduleService.getMember(scheduleId, null);
		//导出操作
		ExportUtils.exportExcel(users, "参与人员", "参与人员", User.class, fileName, response);
	}


	/**
	 * 获取考勤表的考勤记录
	 */
	@GetMapping("/simple")
	public CommonResult exportSimple(ModelMap modelMap,
	                                 HttpServletRequest request,
	                                 HttpServletResponse response,
	                                 @RequestParam("scheduleId") Long scheduleId) {
		Schedule schedule = scheduleService.selectById(scheduleId);
		if (schedule == null) {
			return CommonResult.failure("考勤表不存在");
		}
		exportService.exportSimple(modelMap, request, response, schedule);
		return null;
	}

}
